/*
 * Copyright (C) 2014 naBerTech snc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nabertech.example.android.wearable.sendphoto;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.wearable.Asset;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Model for Main Activity
 */
public class MainModel {
    private static final String TAG = "MainModel";

    // model data
    private boolean mConnected;
    private boolean mWaitingSendPhoto;
    private Bitmap mImageBitmap;

    // Model observer
    private ModelObserver mObserver;

    // Store data
    private static String KEY_CONNECTED = "CONNECTED";
    private static String KEY_WAITING_SEND_PHOTO = "WAITING_SEND_PHOTO";
    private static String KEY_BITMAP = "BITMAP";

    /**
     * The Main model Observer interface.
     */
    public interface ModelObserver {
        public void ModelChanged();
    }

    public MainModel() {
    }

    public void setObserver(ModelObserver observer) {
        mObserver = observer;
    }

    public void setConnected(boolean connected) {
        if (mConnected != connected) {
            mConnected = connected;
            refreshObservers();
        }
    }
    public boolean isConnected() { return mConnected; }

    public void setWaitingSendPhoto(boolean waiting) {
        if (mWaitingSendPhoto != waiting) {
            mWaitingSendPhoto = waiting;
            refreshObservers();
        }
    }
    public boolean isWaitingSendPhoto() { return mWaitingSendPhoto; }

    public void setImageBitmap(Bitmap image) {
        if (mImageBitmap != image) {
            mImageBitmap = image;
            refreshObservers();
        }
    }

    private void refreshObservers() {
        if (mObserver != null)
            mObserver.ModelChanged();
    }

    public Bitmap getImageBitmap() { return mImageBitmap; }

    // Store the model
    public void commit(Bundle bundle) {
        bundle.putBoolean(KEY_CONNECTED, mConnected);
        bundle.putBoolean(KEY_WAITING_SEND_PHOTO, mWaitingSendPhoto);

        if (mImageBitmap != null) {
            ByteArrayOutputStream byteStream = null;
            try {
                byteStream = new ByteArrayOutputStream();
                mImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
                bundle.putByteArray(KEY_BITMAP, byteStream.toByteArray());
            } finally {
                if (null != byteStream) {
                    try {
                        byteStream.close();
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        }
    }

    // Restore the model
    public void restore(Bundle bundle) {
        if (bundle == null)
            return;

        mConnected = bundle.getBoolean(KEY_CONNECTED, false);
        mWaitingSendPhoto = bundle.getBoolean(KEY_WAITING_SEND_PHOTO, false);
        byte[] image = bundle.getByteArray(KEY_BITMAP);
        if (image != null)
            mImageBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);

        refreshObservers();
    }

}
